<?php
$dealer_id = $this->getRequest()->getParam('dealer_id');
$store_id = $this->getRequest()->getParam('store_id');

$brand_id = $this->getRequest()->getParam('brand_id');
$product_name = $this->getRequest()->getParam('product_name');
$status_id = $this->getRequest()->getParam('status');
$page = $this->getRequest()->getParam('page');

$_coreHelper = $this->helper('core');


$resource = Mage::getSingleton('core/resource');
$db = $resource->getConnection('core_read');
$sql = "SELECT eaov.`option_id`,eaov.`value` FROM `eav_attribute_option` eao LEFT JOIN `eav_attribute_option_value` eaov ON eao.`option_id`=eaov.`option_id` WHERE eao.`attribute_id`=139 ORDER BY CONVERT( eaov.`value` USING gbk ) COLLATE gbk_chinese_ci ASC";
$query = $db->query($sql);
$brands = $query->fetchAll();


$session = Mage::getSingleton('admin/session');
$wbid = $session->getUser()->getWebsiteId();
if($wbid)
{
	$store_id = Mage::getModel('core/website')->load($wbid)->getDefaultStore()->getId();
}

$items = $this->getItemCollection();
?>
<div class="content-header">
    <table cellspacing="0">
        <tbody>
        	<tr>
	            <td style="width:50%;"><h3 class="icon-head head-sales-order">商品清单</h3></td>
	            <td class="form-buttons"></td>
        	</tr>
    	</tbody>
	</table>
</div>
<div class="entry-edit">
	<div class="report-header">
		<div class="search-box fieldset">
			<form name="report_form" id="report_form" action="?" method="get">
				<table class="data-table" style="width:800px">
					<?php if(!$session->getUser()->getWebsiteId()):?>
					<tr>
						<?php
						$stores = Mage::getModel('core/store')->getCollection();
						?>
						<td class="label"><label>区域: </label></td>
						<td class="value"><select name="store_id">
						<option value="">--请选择--</option>
							<?php foreach($stores as $store):?>

								<?php if($store->getId()==1) continue; ?>
								<option value="<?php echo $store->getId()?>" <?php if($store_id==$store->getId()) echo 'selected';?>><?php echo $store->getName(); ?></option>
							<?php endforeach;?>
						</select></td>
					</tr>
					<?php endif;?>
					<tr>
						<td class="label"><label>品&nbsp;&nbsp;牌: </label></td>
						<td class="value">
							<select name="brand_id"><option value="">--请选择--</option><?php foreach($brands as $brand):?>
									<option value="<?php echo $brand['option_id']?>" <?php if($brand_id == $brand['option_id']) echo 'selected';?>><?php echo $brand['value'];?></option>
								<?php endforeach;?>
							</select>
						</td>
					</tr>
					<tr>
						<td class="label"><label>商品名: </label></td>
						<td class="value"><input type="text" name="product_name" value="<?php echo $product_name;?>"/></td>
					</tr>
					<?php
						$statuses = array(
							array('value'=>0,'label'=>'全部'),
							array('value'=>1,'label'=>'启用'),
							array('value'=>2,'label'=>'禁用'),
						);
					?>
					<tr>
						<td class="label"><label>商品状态:  </label></td>
						<td class="value"><select name="status">
							<?php foreach($statuses as $status):?>
								<option value="<?php echo $status['value']; ?>" <?php if($status_id==$status['value']) echo 'selected="selected"';?>><?php echo $status['label']; ?></option>
							<?php endforeach;?>
						</select></td>
						<td></td>
					</tr>
					<tr>
						<td colspan="2" style="text-align:right">
							<input type="submit" value="统计" name="apply" id="apply"/> 
							<input type="reset" value="重置" id="reset"/> 
							<input type="submit" value="导出" name="export" id="export"/>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<script type="text/javascript">
		laydate({
		    elem: '#start_date',
		    format: 'YYYY-MM-DD',
		    event: 'focus'
		});
		laydate({
		    elem: '#end_date',
		    format: 'YYYY-MM-DD',
		    event: 'focus'
		});
	</script>
	<div class="grid">
		<div class=""><br/><p>前50条, 更多记录请导出.</p></div>
		<table class="data">
			<thead>
			<tr  class="headings">
				<td>商品名称</td>
				<td>SKU</td>
				<td>分类</td>
				<td>地区</td>
				<td width="8%">品牌</td>
				<td>价格</td>
				<td>库存</td>
				<td>库存状态</td>
				<td>商品状态</td>
			</tr>
			</thead>
			<tbody>
			<?php foreach($items as $item):?>
			<?php $storeIds = $item->getStoreIds();?>
			<?php if($storeIds):?>
				<?php foreach($storeIds as $storeId):?>
				<?php if($storeId==1) continue?>
				<?php if($store_id && $storeId!=$store_id) continue; ?>
				<?php $product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($item->getId()); ?>
						<?php $stockItem = Mage::getModel('cataloginventory/stock_item')->setStoreId($storeId)->loadByProduct($product); ?>
					<?php if($status_id && $product->getStatus()!=$status_id) continue;?>
					<tr>
						<td><?php echo $product->getName(); ?></td>
						<td><?php echo $product->getSku(); ?></td>
						<td><?php echo $this->getCategoryByProduct($product); ?></td>
						<td><?php echo Mage::getModel('core/store')->load($storeId)->getName(); ?></td>
						<td><?php echo $product->getAttributeText('brand'); ?></td>
						<td><?php echo $_coreHelper->formatPrice($product->getPrice(),true,false);?></td>
						<td><?php echo $stockItem->getStockQty(); ?></td>
						<td><?php echo $stockItem->getIsInStock() ? '有货' : '缺货'; ?></td>
						<td><?php echo $product->getStatus()==1?'已启用':'已禁用'; ?></td>
					</tr>
				<?php endforeach;?>
			<?php else:?>
			<?php $product = Mage::getModel('catalog/product')->load($item->getId()); ?>
			<?php if($status_id && $product->getStatus()!=$status_id) continue;?>
			<tr>
				<td><?php echo $product->getName(); ?></td>
				<td><?php echo $product->getSku(); ?></td>
				<td><?php echo $this->getCategoryByProduct($product); ?></td>
				<td></td>
				<td><?php echo $product->getAttributeText('brand'); ?></td>
				<td><?php echo $_coreHelper->formatPrice($product->getPrice(),true,false);?></td>
				<td></td>
				<td></td>
				<td><?php echo $product->getStatus()==1?'已启用':'已禁用'; ?></td>
			</tr>
			<?php endif;?>
			<?php endforeach;?>
			</tbody>
		</table>
		<div class=""><p>前50条, 更多记录请导出.</p></div>
	</div>
</div>
<script type="text/javascript">
(function($){
	// $("#apply").click(function(){
	// 	if($("#r_province_id").val())
	// 	{
	// 		if(!$("#r_city_id").val())
	// 		{
	// 			alert("请选择城市");
	// 			return;
	// 		}
	// 	}
	// 	$("#report_form").submit();
	// });
	// if($('#r_province_id').val()=='') 
 //    {
 //        $('#r_city_id').attr('disabled',true);
 //        $('#r_website_id').attr('disabled',true);
 //    }
	$('#r_province_id').change(function(){      
        $.getJSON('<?php echo $this->getUrl('zgb/ajax/citys')?>?id='+$(this).val(), function(data){
            $('#r_city_id').empty();
            $('#r_website_id').empty();
            var option = $('<option>').val('').text('-请选择-');
            $('#r_website_id').append(option);
            for(var i=0; i<data.length;i++)
            {
                var option = $('<option>').val(data[i].value).text(data[i].label);
                $('#r_city_id').append(option);
            }
            $('#r_city_id').attr('disabled',false);
            $('#r_website_id').attr('disabled',true);
            $('#r_city_id').find('option:first').attr('selected',true);
        });
    });

    $("#r_city_id").change(function(){
        $('#r_website_id').attr('disabled',true);
        $.getJSON('<?php echo $this->getUrl('zgb/ajax/websites')?>?id='+$(this).val(), function(data){
            $('#r_website_id').empty();
            for(var i=0; i<data.length;i++)
            {
                var option = $('<option>').val(data[i].value).text(data[i].label);
                $('#r_website_id').append(option);
            }
            $('#r_website_id').attr('disabled',false);
            $('#r_website_id').find('option:first').attr('selected',true);
        });
    });


})(jQuery);
</script>